using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:06:28
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CCategoriasQuestionarios
   {
       private string IDT_TIQ ;
       private string IDT_CAT ;
       private string DSC_CAT ;
       private string ORDEM;

	   /// <sumary>
       /// Construtor da classe categorias_questionarios
       /// <sumary>
       public CCategoriasQuestionarios()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Categorias_questionarios
       /// <sumary>
       public CCategoriasQuestionarios( string IDT_TIQ, string IDT_CAT, string DSC_CAT, string ORDEM )
       {
          this.IDT_TIQ = IDT_TIQ;
          this.IDT_CAT = IDT_CAT;
          this.DSC_CAT = DSC_CAT;
          this.ORDEM   = ORDEM;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do tipo de questionario
        /// <sumary>
        public string Idt_tiq 
		{
		   get
		   {
		       return this.IDT_TIQ;
		   }
		   set
		   {
		       this.IDT_TIQ = value;
		   }
		}
     	/// <sumary>
        /// Identificador da categoria do tipo de questionario
        /// <sumary>
        public string Idt_cat 
		{
		   get
		   {
		       return this.IDT_CAT;
		   }
		   set
		   {
		       this.IDT_CAT = value;
		   }
		}
     	/// <sumary>
        /// Descricao da categoria do tipo de questionario
        /// <sumary>
        public string Dsc_cat 
		{
		   get
		   {
		       return this.DSC_CAT;
		   }
		   set
		   {
		       this.DSC_CAT = value;
		   }
		}
        /// <sumary>
        /// Ordem da Categoria 
        /// <sumary>
        public string Ordem
        {
            get
            {
                return this.ORDEM;
            }
            set
            {
                this.ORDEM = value;
            }
        }
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Categorias_questionarios.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_CATEGORIAS_QUESTIONARIOS
			                  ( IDT_TIQ, IDT_CAT, DSC_CAT, ORDEM )
						   values
						      ( :IDT_TIQ, :IDT_CAT, :DSC_CAT, :ORDEM )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_TIQ",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_CAT",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_CAT",OracleType.VarChar));
                command.Parameters.Add(new OracleParameter("ORDEM", OracleType.Number)); 
				
                command.Parameters["IDT_TIQ"].Value = this.IDT_TIQ;
                command.Parameters["IDT_CAT"].Value = this.IDT_CAT;
                command.Parameters["DSC_CAT"].Value = this.DSC_CAT;
                command.Parameters["ORDEM"].Value = this.ORDEM;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Categorias_questionarios.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_CATEGORIAS_QUESTIONARIOS set 
			                   DSC_CAT = :DSC_CAT
						   where
						       IDT_TIQ = :IDT_TIQ and IDT_CAT = :IDT_CAT ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_TIQ",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_CAT",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_CAT",OracleType.VarChar)); 
				
                command.Parameters["IDT_TIQ"].Value = this.IDT_TIQ;
                command.Parameters["IDT_CAT"].Value = this.IDT_CAT;
                command.Parameters["DSC_CAT"].Value = this.DSC_CAT;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Categorias_questionarios.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_CATEGORIAS_QUESTIONARIOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_TIQ", DbType.Int32, this.IDT_TIQ));
            tab.adicionarFiltro(new Coluna("IDT_CAT", DbType.Int32, this.IDT_CAT));
            tab.delete();
        }
		
		static public DataTable ObterTodos(string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_CATEGORIAS_QUESTIONARIOS order by DSC_CAT ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CCategoriasQuestionarios PesquisaporCodigo(string idt_tiq, string idt_cat, string login)
        {
            CCategoriasQuestionarios obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_CATEGORIAS_QUESTIONARIOS 
                            WHERE IDT_TIQ = :IDT_TIQ and IDT_CAT = :IDT_CAT";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_TIQ", DbType.String, idt_tiq);
				qy.AdicionarParametro(":IDT_CAT", DbType.String, idt_cat);
               
				qy.Executar();
                obj = new CCategoriasQuestionarios(qy.ObterCampo("IDT_TIQ"), qy.ObterCampo("IDT_CAT"), qy.ObterCampo("DSC_CAT"), qy.ObterCampo("ORDEM"));
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CCategoriasQuestionarios();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string idt_tiq, string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_CAT(:idt_tiq) IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
                qy.AdicionarParametro("idt_tiq", DbType.Int32, idt_tiq);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }

        static public DataTable ObterPorIDT_TIQ(string idt_tiq, string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_CATEGORIAS_QUESTIONARIOS where IDT_TIQ = :IDT_TIQ order by DSC_CAT ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_TIQ", OracleType.Number));
                command.Parameters["IDT_TIQ"].Value = idt_tiq;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
        }

        /// <summary>
        /// Alterando um Questoes.
        /// </summary>
        public void AlterarOrdemCategoria(string idt_tiq, string idt_cat, string acao, string login)
        {
            try
            {
                OracleCommand command = new OracleCommand("P_ALTERAR_ORDEM_CATEGORIA", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo));
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new OracleParameter("p_idt_tiq", OracleType.Number));
                command.Parameters.Add(new OracleParameter("p_idt_cat", OracleType.Number));
                command.Parameters.Add(new OracleParameter("p_acao", OracleType.Char));
                command.Parameters["p_idt_tiq"].Value = Convert.ToInt16(idt_tiq);
                command.Parameters["p_idt_cat"].Value = Convert.ToInt16(idt_cat);
                command.Parameters["p_acao"].Value = acao;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "P_ALTERAR_ORDEM_CATEGORIA");
            }
        }


       static public DataTable ObterCategoriasPorQuestionario(string idt_qst, string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @" select idt_tiq, idt_cat, dsc_cat, ordem
                              from serix.srx_categorias_questionarios c
                              join serix.srx_tipos_questionarios t on (c.idt_tiq = t.idt_tiq)
                              join serix.srx_questionarios q on (t.idt_tiq = q.idt_tiq)
                             where q.idt_qst = :idt_qst
                               and exists ( select 1 from serix.srx_questoes q2
                                             where q2.idt_tiq = c.idt_tiq
                                               and q2.idt_cat = c.idt_cat  
                                          )
                             order by ordem";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_QST", OracleType.Number));
                command.Parameters["IDT_QST"].Value = idt_qst;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
      
    }
}	

